package smartcode.privacy;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.aliang.auto.R;
import com.just.agentweb.AgentWeb;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import app.auto.AndroidInstance;
import app.auto.runner.base.SPrefUtil;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.utility.ToastUtil;
import smartcode.util.DialogUtils;


public class PrivacyHelper extends AndroidInstance {

    public static final String serve = "http://ewsh.cc/staticassets/yonghuxieyi.txt";
    public static final String privacyterm = "http://ewsh.cc/staticassets/aitunhuo.txt";


    public void showDescOppo(FunCallback funcallback) {
        String privacy = (String) SPrefUtil.getValue(PrivacyHelper.class.getSimpleName());

        if (privacy.equals("true")) {
            if (funcallback != null) {
                funcallback.onCallback(null, null);
            }
        } else {
            DialogUtils d = DialogUtils.getInstance().with(getContext())
                    .setlayoutPosition(Gravity.CENTER)
                    .setlayoutAnimaType(DialogUtils.ANIM_FADE_IN)
                    .setlayoutId(R.layout.layout_yinsi_oppo)
                    .setlayoutPading(0, 0, 0, 0)
                    .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                        @Override
                        public void getChildView(View view, int layoutResId) {
                            TextView tv_xieyi = view.findViewById(R.id.tv_xieyi);
                            TextView tv_yinsi = view.findViewById(R.id.tv_yinsi);
                            final TextView tv_cancal = view.findViewById(R.id.tv_cancal);
                            final TextView tv_sure = view.findViewById(R.id.tv_sure);
                            TextView agree_txt = view.findViewById(R.id.agree_txt);

                            final CheckBox agree = view.findViewById(R.id.agree);
                            agree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                                    if (isChecked) {
//                                        tv_sure.setVisibility(View.VISIBLE);
//                                        tv_cancal.setVisibility(View.GONE);
//                                    } else {
//                                        tv_sure.setVisibility(View.GONE);
//                                        tv_cancal.setVisibility(View.VISIBLE);
//                                    }
                                }
                            });
                            agree_txt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    agree.performClick();
                                }
                            });
                            tv_sure.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (!agree.isChecked()) {
                                        ToastUtil.live(getContext()).toast("请先阅读并勾选完整协议");
                                        return;
                                    }
                                    DialogUtils.dismiss();
                                    SPrefUtil.putValue(PrivacyHelper.class.getSimpleName(), "true");
                                    if (funcallback != null) {
                                        funcallback.onCallback(null, null);
                                    }
                                }
                            });
                            tv_cancal.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtils.dismiss();
                                    PrivacyHelper.this.finish();
                                }
                            });
                            tv_xieyi.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    BaseWebViewData webData = new BaseWebViewData();
                                    webData.title = "用户协议";
                                    webData.largeSize = true;
                                    webData.content = serve;

                                    startAct(getContext(), WebViewActivity.class, webData);
                                }
                            });
                            tv_yinsi.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    BaseWebViewData webData = new BaseWebViewData();
                                    webData.title = "隐私协议";
                                    webData.largeSize = true;
                                    webData.content = PrivacyHelper.privacyterm;
                                    startAct(getContext(), WebViewActivity.class, webData);
                                }
                            });
                        }
                    }).show().setCancelable(false);
        }
    }

    public static void startAct(Context context, Class<WebViewActivity> cls, BaseWebViewData webData) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        if (webData != null) {
            intent.putExtra("Data", (Serializable) webData);
        }

        if (-1 != -1) {
            ((Activity) context).startActivityForResult(intent, -1);
        } else {
            context.startActivity(intent);
        }
    }

    private void next() {


    }

    private void finish() {
        ((Activity) getContext()).finish();

    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
